package com.chris.homecare.components
{
	import com.chris.homecare.TextureAtlasManager;
	import com.chris.homecare.utils.CommonUtils;
	
	import flash.geom.Point;
	
	import feathers.controls.ImageLoader;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	
	import starling.display.Image;
	import starling.display.Quad;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;

	[Event(name="triggerActive",type="starling.events.Event")]
	
	public class CalenderDateComponent extends LayoutGroup
	{
		public static const TRIGGER_ACTIVE:String = "triggerActive";
		
		private var backgroundImg:Quad;
		private var backgroundColor:uint;
		private var eventIndicatorImage:ImageLoader;
		
		private var contentLabel:Label;
		private var centerContent:String;
		public var activeDate:Date;
		private var hasEvent:Boolean;
		private var isExpired:Boolean;
		
		private var touchPointId:int = -1;
		private static const HELPER_TOUCHES_VECTOR:Vector.<Touch> = new <Touch>[];
		private static const HELPER_POINT:Point = new Point();
		
		public static const SELECTED:uint = 0x64A950;
		public static const DESELECTED:uint = 0x999999;
		public static const SUNDAY:uint = 0x006600;
		public static const OTHERDAYS:uint = 0x666666;
		public static const DISABLED:uint = 0xBBBBBB;
		public static const EVENTDAYS:uint = 0x1E90FF;
		
		public function CalenderDateComponent(content:String, backgroundColor:uint=DESELECTED, hasEvent:Boolean = false, isExpired:Boolean = false)
		{
			this.backgroundColor = backgroundColor;
			this.centerContent = content;
			this.addEventListener(TouchEvent.TOUCH, touchHandler);
			this.hasEvent = hasEvent;
			this.isExpired = isExpired;
			super();
		}
		
		override protected function initialize():void
		{
			backgroundImg = new Quad(1,1,backgroundColor);
			addChildAt(backgroundImg,0);
			
			contentLabel = new Label();
			contentLabel.text = centerContent;
			addChild(contentLabel);
			
			eventIndicatorImage = new ImageLoader();
			addChild(eventIndicatorImage);
			
			if(this.hasEvent)
			{
				if(isExpired) eventIndicatorImage.source = TextureAtlasManager.getAtlas().getTexture("past_event_indicator");
				
				else eventIndicatorImage.source = TextureAtlasManager.getAtlas().getTexture("future_event_indicator");
			}
			
			super.initialize();
		}
		
		override protected function draw():void
		{
			
			super.draw();
			this.width = backgroundImg.width = isNaN(this.explicitWidth)? (contentLabel.width + 20) : this.explicitWidth;
			this.height = backgroundImg.height = isNaN(this.explicitHeight)? (contentLabel.height + 25*CommonUtils.scaleValue) : this.explicitHeight;
			
			contentLabel.x = (this.actualWidth - contentLabel.width)/2;
			contentLabel.y = (this.actualHeight - contentLabel.height)/2;
			
			eventIndicatorImage.x = contentLabel.x + contentLabel.width;
			eventIndicatorImage.y = contentLabel.y + contentLabel.height - 5;
		}
		
		public function replaceBackground(imageColor:int = DESELECTED):void
		{
			if(backgroundImg) this.removeChild(backgroundImg);
			
			if(imageColor == SELECTED)
			{
				backgroundImg = new Quad(1,1,imageColor);
			}
			//else if(hasEvent) backgroundImg = new Quad(1,1, EVENTDAYS);
			
			else backgroundImg = new Quad(1,1,imageColor);
			
			addChildAt(backgroundImg,0);
		}
		
		private function touchHandler(event:TouchEvent):void
		{
			if(!this._isEnabled) return;

			const touches:Vector.<Touch> = event.getTouches(this, null, HELPER_TOUCHES_VECTOR);
			if(touches.length == 0) return;
			
			var touch:Touch;
			
			if(touchPointId >= 0)
			{
				for each(var currentTouch:Touch in touches)
				{
					if(currentTouch.id == touchPointId)
					{
						touch = currentTouch;
						break;
					}
				}
				
				if(!touch)
				{
					HELPER_TOUCHES_VECTOR.length = 0;
					return;
				}
				
				if(touch.phase == TouchPhase.ENDED)
				{
					touchPointId = -1;
					touch.getLocation(this, HELPER_POINT);
					var isInBounds:Boolean = this.hitTest(HELPER_POINT, true) != null;
					if(isInBounds)
					{
						replaceBackground(SELECTED);
						this.dispatchEventWith(TRIGGER_ACTIVE, true, centerContent);
					}
				}
			}
			
			else
			{
				for each(touch in touches)
				{
					if(touch.phase == TouchPhase.BEGAN)
					{
						this.touchPointId = touch.id;
						break;
					}
				}
			}
			
			HELPER_TOUCHES_VECTOR.length = 0;
		}
	}
}